package com.skirmisher.inspection.common.service.impl;

import com.skirmisher.inspection.common.entity.InsCodeConfig;
import com.skirmisher.inspection.common.mapper.InsCodeConfigMapper;
import com.skirmisher.inspection.common.service.SystemCodeCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @ClassName SystemCodeCacheServiceImpl
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/10 下午8:45
 * @Version 1.0
 */
@Slf4j
@Service
public class SystemCodeCacheServiceImpl implements SystemCodeCacheService {

    public static Map<String,InsCodeConfig> codeMap = new HashMap<>();

    public boolean isUpdate = false;

    @Autowired
    private InsCodeConfigMapper insCodeConfigMapper;

    @PostConstruct
    @Override
    public void init() {
        log.info("编码已经开始加载");
        List<InsCodeConfig> insCodeConfigs = insCodeConfigMapper.listAll();
        if (insCodeConfigs!= null && insCodeConfigs.size() >= 0){
            for (InsCodeConfig insCodeConfig : insCodeConfigs) {
                String codeIndex = insCodeConfig.getCodeIndex();
                codeMap.put(codeIndex,insCodeConfig);
                System.out.println(codeIndex);
            }
        }
        log.info("编码加载结束");

    }

    @Override
    public String getNextCode(String indexString) {

        String dateTime = dateString();
        //判断是否存在该编码的索引
        boolean b = codeMap.containsKey(indexString);
        if (b){
            InsCodeConfig insCodeConfig = codeMap.get(indexString);
            Integer nowCode = insCodeConfig.getNowCode();
            String format = String.format("%04d", nowCode + 1);
            insCodeConfig.setNowCode(nowCode + 1);

            isUpdate = true;
            return dateTime+format;
        }else {
            InsCodeConfig code = new InsCodeConfig();
            code.setCodeIndex(indexString);
            code.setId(UUID.randomUUID().toString());
            code.setNowCode(1);
            String format = String.format("%04d", 1);
            insCodeConfigMapper.insert(code);
            codeMap.put(indexString,code);
            isUpdate = true;
            return dateTime+format;
        }
    }

    @Override
    public void updateCache() {

        if (isUpdate){
            log.info("更新编码表");
            for (String index : codeMap.keySet()) {
                InsCodeConfig insCodeConfig = codeMap.get(index);
                insCodeConfigMapper.updateIgnoreNull(insCodeConfig);
                isUpdate = false;
            }
            log.info("更新编码表结束");
        }
    }

    @Override
    public String getNoDateCode(String indexString) {

        String dateTime = dateString();
        //判断是否存在该编码的索引
        boolean b = codeMap.containsKey(indexString);
        if (b){
            InsCodeConfig insCodeConfig = codeMap.get(indexString);
            Integer nowCode = insCodeConfig.getNowCode();
            String format = String.format("%06d", nowCode + 1);
            insCodeConfig.setNowCode(nowCode + 1);
            isUpdate = true;
            return indexString + format;
        }else {
            InsCodeConfig code = new InsCodeConfig();
            code.setCodeIndex(indexString);
            code.setId(UUID.randomUUID().toString());
            code.setNowCode(1);
            String format = String.format("%04d", 1);
            insCodeConfigMapper.insert(code);
            codeMap.put(indexString,code);
            isUpdate = true;
            return indexString + format;
        }

    }

    private String dateString(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
        return now.format(yyyyMMdd);
    }
}
